<template>
	<view class="content mainpadding">
		<!-- 持卡人 -->
		<view class="mainpadding ffffff border margin_bottom3 flexbetween">
			<view class="titletext xiaohei margin_right3">开户人姓名</view>
			<input class="titletext textright flex1" type="text" placeholder="请输入开户人姓名" v-model="username">
		</view>
		<view class="mainpadding ffffff border margin_bottom3 flexbetween">
			<view class="titletext xiaohei margin_right3">开户人身份证号</view>
			<input class="titletext textright flex1" type="text" placeholder="请输入开户人身份证号" v-model="idcard">
		</view>
		<view class="mainpadding ffffff border margin_bottom3 flexbetween">
			<view class="titletext xiaohei margin_right3">预留手机号</view>
			<input class="titletext textright flex1" type="number" placeholder="请输入预留手机号" v-model="mobile">
		</view>
		<!-- 银行卡号 -->
		<view class="mainpadding ffffff border margin_bottom3 flexbetween">
			<view class="titletext xiaohei margin_right3">银行卡号</view>
			<input class="titletext textright flex1" type="number" placeholder="请输入银行卡号" v-model="cardno">
		</view>
		<view class="mainpadding ffffff border margin_bottom3 flexbetween" @click="yhdhshow=true">
			<view class="titletext xiaohei margin_right3">所属银行</view>
			<view class="flexright">
				<input class="titletext textright flex1" type="text" placeholder="请选择所属银行" disabled=""
					v-model="bank_name">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="mainpadding ffffff border margin_bottom3 flexbetween">
			<view class="titletext xiaohei margin_right3">注册手机号</view>
			<input class="titletext textright flex1" type="number" placeholder="请输入注册手机号" disabled="" v-model="mymobile">
		</view>
		<view class="mainpadding ffffff border margin_bottom3 flexbetween">
			<uni-captcha scene="bankcer" v-model="imgcode"></uni-captcha>
		</view>
		<view class="mainpadding ffffff border margin_bottom3 flexbetween">
			<view class="titletext xiaohei margin_right3">验证码</view>
			<view class="flexright">
				<input class="titletext textright flex1" type="number" placeholder="输入验证码"
					v-model="yzm_code">
				<view class="xiaolan strongtext textcenter"  style="min-width: 150rpx;" @click="sendyzm" v-show="codetext == 1">获取验证码</view>
				<view class="xiaolan strongtext textcenter"  style="min-width: 150rpx;" v-show="codetext == 2">{{second}}s后重新获取</view>
				<view class="xiaolan strongtext textcenter"  style="min-width: 150rpx;" @click="sendyzm" v-show="codetext == 3">重新发送</view>
			</view>
		</view>
		<view class="mainpadding">
			<view class="bigbtn" @click="submit()">
				确认
			</view>
		</view>
		<u-picker :show="yhdhshow" :columns="yhdhcolumns" title="" @cancel="yhdhshow=false" @confirm="yhdhconfirm"
			keyName="name"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				username: "",
				idcard: "",
				mobile: "",
				cardno: "",
				bank_id: "",
				bank_name: "",
				yzm_code: "",
				codetext: 1,
				second: 59,
				listid: "",
				mymobile:"",
				yhdhcolumns: [],
				yhdhshow: false,
				imgcode:""
			}
		},
		onLoad(options) {
			if (options.id) {
				this.listid = options.id
				uni.setNavigationBarTitle({
					title:"编辑银行卡"
				})
				this.getDetail()
			}
			this.getyhdh()
			this.init()
		},
		methods: {
			init() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.mymobile = res.data.mobile 
				})
			},
			sendyzm() {
				let _this = this;
				if (this.imgcode=="") {
					httpRequest.toast("请输入图形验证码!")
					return;
				}
				let device = uni.getSystemInfoSync();
				httpRequest.request('/api/sms/send', 'POST', {
					mobile: this.mymobile,
					event: 'bankcer',
					code: this.imgcode,
					deviceId: device.deviceId,
					scene: 'bankcer',
					// tgbool:1
				}, false, true, false).then(res => {
					if(res.code == 1){
						_this.codetext = 2;
						_this.second = 59;
						
						let timer = setInterval(function(){
							if(_this.second > 1){
								_this.second--;
							}else{
								clearInterval(timer);
								_this.codetext = 3;
							}
						},1000)
						httpRequest.toast('发送成功')
					}else{
						httpRequest.toast(res.msg)
					}
				})
			},
			getyhdh() {
				httpRequest.request('/api/user/bankIndex', 'GET', {}).then(res => {
					this.yhdhcolumns.push(res.data)
				})
			},
			yhdhconfirm(e) {
				this.bank_name = e.value[0].name
				this.bank_id = e.value[0].id
				this.yhdhshow = false
			},
			getDetail() {
				httpRequest.request('/api/user/userBankDetail', 'GET', {
					id: this.listid,
				}).then(res => {
					let data = res.data
					this.username=data.username
					this.idcard=data.idcard
					this.mobile=data.mobile
					this.cardno=data.cardno
					this.bank_id=data.bank_id
					this.bank_name=data.bank.name
				})
			},
			submit() {
				if (this.username == "") {
					httpRequest.toast("请输入开户人姓名")
					return false
				}
				if (this.idcard == "") {
					httpRequest.toast("请输入开户人身份证号")
					return false
				}
				if (this.mobile == "") {
					httpRequest.toast("请输入预留手机号")
					return false
				}
				if (this.cardno == "") {
					httpRequest.toast("请输入银行卡号")
					return false
				}
				if (this.bank_id == "") {
					httpRequest.toast("请选择所属银行")
					return false
				}
				if (this.yzm_code == "") {
					httpRequest.toast("请输入验证码")
					return false
				}
				let data = {
					username: this.username,
					idcard: this.idcard,
					mobile: this.mobile,
					cardno: this.cardno,
					bank_id: this.bank_id,
					captcha: this.yzm_code
				}
				let url = "/api/user/addUserBank"
				if (this.listid) {
					url = "/api/user/editUserBank"
					this.$set(data, "id", this.listid)
				}
				httpRequest.request(url, 'POST', data).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.margin_right3{
		width: 250rpx;
	}
	.content {
		padding-bottom: 150rpx;
	}

	.noSelect {
		width: 138rpx;
		height: 69rpx;
		line-height: 69rpx;
		text-align: center;
		background: rgba(102, 102, 102, .1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid rgba(102, 102, 102, .1);
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		margin-right: 30rpx;
		color: #666666;
	}

	.select {
		width: 138rpx;
		height: 69rpx;
		line-height: 69rpx;
		text-align: center;
		background: rgba(255, 133, 5, 0.1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid #FF8505;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FF8505;
		margin-right: 30rpx;
		position: relative;
	}

	.select::after {
		content: '';
		width: 25rpx;
		height: 20rpx;
		background-image: url("/static/images/mine/invoice_select.png");
		background-size: 100% 100%;
		position: absolute;
		right: 0;
		bottom: 0;
	}

	.item_image_box {
		width: 33.33%;
	}

	.item_image {
		position: relative;
		width: 180rpx;
		height: 180rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>